<template>
	<view>
		<web-view :src="webViewUrl" @message="getMessage" @postMessage="getPostMessage($event)"></web-view>
	</view>
</template>

<script>
	import {
		shareFriend,
		shareMoment,
		saveAlbum
	} from '../../utility/share.js'
	import {saveImg} from '../../utility/download.js'
	export default {
		data() {
			return {
				webViewUrl: '',
				title: ''
			}
		},
		methods: {
			getPostMessage(data) {
				console.log('接收到消息1111~~', data)
			},
			getMessage(data) {
				console.log('接收到消息~~', data.detail.data[0])
				const {
					action,
					invitationCode
				} = data.detail.data[0]
				if (action) {
					//未实名先去实名
					// if(action == 'realName'){
					// 	uni.navigateTo({
					// 		url:'/pages/user/realName'
					// 	})
					// 	return false
					// }
					//忘记密码去进行修改
					if (action == 'realPassword') {
						uni.navigateTo({
							url: '/pages/safe/index'
						})
						return false
					}
					//前往修改地址
					if (action == 'realAddress') {
						if (data.detail.data[0].hasOwnProperty('id')) {
							uni.navigateTo({
								url: '/pages/user/setAddress?id=' + data.detail.data[0].id
							})
							return false
						}
						uni.navigateTo({
							url: '/pages/user/accountAddress'
						})
						return false
					}
					//返回小镇页面
					if (action == 'realTownIndex') {
						uni.switchTab({
							url: '/pages/town/index'
						})
						return false
					}
					//保存图片
					if(action.name == 'downLoadImg'){
						uni.showModal({
							title:'接受到消息',
							content:JSON.stringify(action)
						})
						saveImg(action.url)
						return false
					}
					uni.showActionSheet({
						itemList: ['分享给朋友', '分享到朋友圈', '保存到相册'],
						success: function(res) {
							console.log(res)
							switch (res.tapIndex) {
								case 0:
									shareFriend(action)
									break;
								case 1:
									shareMoment(action)
									break;
								case 2:
									saveAlbum(action)
									break;
								default:
									break;
							}
						},
					})
				} else {
					uni.showToast({
						title: '暂未获得图片地址',
						icon: 'none'
					})
				}
			}
		},
		onLoad(e) {
			let url;
			if (e.pageType) {
				url = `${e.url}&token=${e.token}&pageType=${e.pageType}`
			} else if (e.token) {
				url = `${e.url}&token=${e.token}`
			} else {
				url = `${e.url}`
			}
			console.log(412, url)
			this.webViewUrl = url
			this.title = e.title || ''
			setTimeout(() => {
				uni.setNavigationBarTitle({
					title: this.title
				});
			}, 500)
		}
	}
</script>

<style>
</style>
